/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.textdescriptor.definitions;

import java.util.logging.Logger;

import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import com.acarter.scenemonitor.textdescriptor.A_TextDescriptor;
import com.jme.bounding.BoundingVolume;
import com.jme.scene.Spatial;

/**
 * @author Carter
 * 
 */
public class SpatialDescriptor extends A_TextDescriptor {

	/**  */
	public static final String LOGGER_NAME = "com.acarter.scenemonitor.textdescriptor.definitions.SpatialDescriptor";

	/**  */
	private static final Logger LOGGER = Logger.getLogger(SpatialDescriptor.LOGGER_NAME);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.acarter.scenemonitor.descriptor.A_MonitorDescriptor#UpdateInfo(javax.swing.text.StyledDocument,
	 *      java.lang.Object)
	 */
	@Override
	public void updateInfo(StyledDocument infoDocument, Object object) {

		if ((object != null) && (object instanceof Spatial)) {
			
			Spatial spatial = (Spatial)object;

			try {
				clearDocument(infoDocument);
				writeString(infoDocument, spatial.getName() + "\n", "bold");
				writeString(infoDocument, "Spatial\n\n", "small");
				
				updateInheritedInfo(infoDocument, spatial);
			}
			catch (BadLocationException ble) {
				LOGGER.warning("Could not insert info text.");
			}

		}
	}

	/**
	 * 
	 * @param infoDocument
	 * @param spatial
	 * @throws BadLocationException
	 */
	protected void updateInheritedInfo(StyledDocument infoDocument, Object object) throws BadLocationException {
		
		if(object instanceof Spatial) {
			
			Spatial spatial = (Spatial)object;
	
			writeString(infoDocument, "Local Translation:\n", "bold");
			writeString(infoDocument, toString(spatial.getLocalTranslation()) + "\n", "regular");
			writeString(infoDocument, "Local Rotation:\n", "bold");
			writeString(infoDocument, toString(spatial.getLocalRotation()) + "\n", "regular");
			writeString(infoDocument, "Local Scale:\n", "bold");
			writeString(infoDocument, toString(spatial.getLocalScale()) + "\n\n", "regular");
			
			writeString(infoDocument, "Lock Bounds: ", "bold");
			writeString(infoDocument, (((spatial.getLocks() & Spatial.LOCKED_BOUNDS) != 0 ) ? "true" : "false") + "\n", "regular");
			writeString(infoDocument, "Lock Meshes: ", "bold");
			writeString(infoDocument, (((spatial.getLocks() & Spatial.LOCKED_MESH_DATA) != 0 ) ? "true" : "false") + "\n", "regular");
			writeString(infoDocument, "Lock Shadows: ", "bold");
			writeString(infoDocument, (((spatial.getLocks() & Spatial.LOCKED_SHADOWS) != 0 ) ? "true" : "false") + "\n", "regular");
			writeString(infoDocument, "Lock Transforms: ", "bold");
			writeString(infoDocument, (((spatial.getLocks() & Spatial.LOCKED_TRANSFORMS) != 0 ) ? "true" : "false") + "\n", "regular");
			writeString(infoDocument, "Lock Branch: ", "bold");
			writeString(infoDocument, (((spatial.getLocks() & Spatial.LOCKED_BRANCH) != 0 ) ? "true" : "false") + "\n\n", "regular");
			
			writeString(infoDocument, "Bounding Volume: ", "bold");
			writeString(infoDocument, E_BoundingVolume.getConstant(spatial.getWorldBound()) + "\n\n", "regular");
	
			writeString(infoDocument, "Cull Hint: ", "bold");
			writeString(infoDocument, spatial.getCullHint() + "\n", "regular");
			writeString(infoDocument, "Light Combine: ", "bold");
			writeString(infoDocument, spatial.getLightCombineMode() + "\n", "regular");
			writeString(infoDocument, "Texture Combine: ", "bold");
			writeString(infoDocument, spatial.getTextureCombineMode() + "\n", "regular");
			writeString(infoDocument, "Render Queue: ", "bold");
			writeString(infoDocument, E_RenderQueueMode.getConstant(spatial.getRenderQueueMode()) + "\n", "regular");
			writeString(infoDocument, "Normals: ", "bold");
			writeString(infoDocument, spatial.getNormalsMode() + "\n\n", "regular");
	
			writeString(infoDocument, "Collidable: ", "bold");
			writeString(infoDocument, spatial.isCollidable() + "\n", "regular");
			writeString(infoDocument, "Z Order: ", "bold");
			writeString(infoDocument, Integer.toString(spatial.getZOrder()) + "\n", "regular");	
		}
	}

	/**
	 * Enumerates the permissible render queue mode.
	 * 
	 * @author Andrew Carter
	 */
	protected enum E_RenderQueueMode {

	    /** The Spatial will inherit its render queue state from its parent. */
	    QUEUE_INHERIT ("QUEUE_INHERIT", 0),

	    /** The Spatial will skip render queueing. */
	    QUEUE_SKIP("QUEUE_SKIP", 1),

	    /** The Spatial will render in the opaque bucket. */
	    QUEUE_OPAQUE("QUEUE_OPAQUE", 2),

	    /** The Spatial will render in the transparent bucket. */
	    QUEUE_TRANSPARENT("QUEUE_TRANSPARENT", 3),

	    /** The Spatial will render in the ortho bucket. */
	    QUEUE_ORTHO("QUEUE_ORTHO", 4);

		/** String representation of the enum value */
		private String name;

		/** Integer value of the enum value */
		private int value;

		/**
		 * Private constructor.
		 * 
		 * @param value
		 *            String literal of the enum
		 */
		private E_RenderQueueMode(String name, int value) {

			this.name = name;
			this.value = value;
		}

		/**
		 * Returns the enum constant matching the name string.
		 * 
		 * @param name
		 *            string value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_RenderQueueMode getConstant(String name) {

			for (E_RenderQueueMode constant : E_RenderQueueMode.values()) {

				if (constant.toString().compareToIgnoreCase(name) == 0)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the enum constant matching the type value.
		 * 
		 * @param type
		 *            type value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_RenderQueueMode getConstant(int type) {

			for (E_RenderQueueMode constant : E_RenderQueueMode.values()) {

				if (constant.getType() == type)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the string value of the constant.
		 */
		public String toString() {

			return name;
		}

		/**
		 * Returns the integer value of the constant.
		 * 
		 * @return
		 */
		public int getType() {

			return value;
		}
	}
	
	/**
	 * Enumerates the permissible bounding volume types.
	 * 
	 * @author Andrew Carter
	 */
	protected enum E_BoundingVolume {

		NONE("None", ""),

		BOX("Bounding Box", "BoundingBox"),

		CAPSULE("Bounding Capsule", "BoundingCapsule"),

		SPHERE("Bounding Sphere", "BoundingSphere"),

		ORIENTEDBOX("Oriented Box", "OrientedBoundingBox");

		/** String representation of the enum value */
		private String name;

		/** Integer value of the enum value */
		private String value;

		/**
		 * Private constructor.
		 * 
		 * @param value
		 *            String literal of the enum
		 */
		private E_BoundingVolume(String name, String value) {

			this.name = name;
			this.value = value;
		}

		/**
		 * Returns the enum constant matching the name string.
		 * 
		 * @param name
		 *            string value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_BoundingVolume getConstant(String name) {

			for (E_BoundingVolume constant : E_BoundingVolume.values()) {

				if (constant.toString().compareToIgnoreCase(name) == 0)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the enum constant matching the type value.
		 * 
		 * @param type
		 *            type value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_BoundingVolume getConstant(BoundingVolume type) {

			if (type != null) {
				
				for (E_BoundingVolume constant : E_BoundingVolume.values()) {

					if (constant.getType().compareToIgnoreCase(type.getClass().getSimpleName()) == 0)
						return constant;
				}
			}
			
			return NONE;
		}

		/**
		 * Returns the string value of the constant.
		 */
		public String toString() {

			return name;
		}

		/**
		 * Returns the integer value of the constant.
		 * @return 
		 */
		public String getType() {

			return value;
		}
	}
}